 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=219 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:26:57 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Consider Controllers for redirects
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='header,redirect,welcome,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Consider Controllers for redirects</div>

<div class='main-body'>
 
<br>
HTTP provides <a href='http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3'>redirection headers</a>.
A main use case for these headers is to instruct browsers that a page has moved to a new URL, either temporarily or permanently.
When a page is moved, any links to the old URL will be broken, unless a redirect is provided.

<P>Here is a snippet which performs a permanent redirection, using an <tt>HttpServletResponse</tt> object:
<PRE>
aResponse.<a href='http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletResponse.html#setStatus(int)'>setStatus</a>(HttpServletResponse.SC_MOVED_PERMANENTLY);
aResponse.<a href='http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletResponse.html#setHeader(java.lang.String,%20java.lang.String)'>setHeader</a>("Location", "www.javapractices.com/source/SourceAction.do");
</PRE>

In the case of a <em>temporary</em> redirection, another style can be used instead, if desired : 
<PRE>
aResponse.<a href='http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletResponse.html#sendRedirect(java.lang.String)'>sendRedirect</a>("www.javapractices.com/source/SourceAction.do");
</PRE>

<P>Using a Controller servlet to perform redirections is often an attractive option. 

<p><b>Example</b>

<P>The Servlet API allows for <tt>welcome-file</tt> entries in <tt>web.xml</tt>.
These items handle requests which specify only a <em>directory</em>, instead of a specific file or action.

<P>A common requirement is to serve welcome files using a servlet instead of a static page.
It is true that a static page can indeed perform a redirect. However, that style seems to be slower.
As well, it forces the quick display of an intermediate page before the "real" one, which seems to be a less pleasing experience for the user.

<P>The javapractices.com site uses a redirection Controller to serve its home page. 
Here are the relevant entries in its <tt>web.xml</tt>:

<PRE>
&lt;servlet&gt;
 &lt;servlet-name&gt;RedirectWelcomeFile&lt;/servlet-name&gt;
 &lt;description&gt;
  Redirects directory requests to the home page Action.
 &lt;/description&gt;
 &lt;servlet-class&gt;hirondelle.jp.redirect.WelcomeFileController&lt;/servlet-class&gt;
  &lt;init-param&gt;  
   &lt;param-name&gt;Destination&lt;/param-name&gt;
   &lt;param-value&gt;http://www.javapractices.com/home/HomeAction.do&lt;/param-value&gt;
    &lt;description&gt;
     The URL of the home page, as an Action.
    &lt;/description&gt;
  &lt;/init-param&gt;  
 &lt;/servlet&gt;

&lt;servlet-mapping&gt;
 &lt;servlet-name&gt;RedirectWelcomeFile&lt;/servlet-name&gt;
 &lt;url-pattern&gt;/welcome&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;

&lt;!-- the welcome-file points to the servlet, not to a file --&gt;
&lt;!-- note the slash does not appear here --&gt;
&lt;welcome-file-list&gt;
 &lt;welcome-file&gt;welcome&lt;/welcome-file&gt;
&lt;/welcome-file-list&gt;
</PRE>

The actual Controller class is fairly simple. 
Note that it reads in the <tt>Destination</tt> setting from <tt>web.xml</tt> :
<br>
<PRE>

<span class='keyword'>package</span> hirondelle.jp.redirect;

<span class='keyword'>import</span> hirondelle.web4j.util.Util;
<span class='keyword'>import</span> java.io.IOException;
<span class='keyword'>import</span> java.util.logging.Logger;
<span class='keyword'>import</span> javax.servlet.ServletConfig;
<span class='keyword'>import</span> javax.servlet.ServletException;
<span class='keyword'>import</span> javax.servlet.http.HttpServlet;
<span class='keyword'>import</span> javax.servlet.http.HttpServletRequest;
<span class='keyword'>import</span> javax.servlet.http.HttpServletResponse;

<span class='comment'>/**
* Redirect requests for the home page to a specific Action.
* 
* &lt;P&gt;Configuration in web.xml :&lt;br&gt;
* This Controller servlet is mapped to '/welcome', and a corresponding welcome-file entry 
* is listed as 'welcome', without the leading slash.
*/</span>
<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> WelcomeFileController  <span class='keyword'>extends</span> HttpServlet {
  
  <span class='keyword'>@Override</span> <span class='keyword'>public</span> <span class='keyword'>void</span> init(ServletConfig aConfig) <span class='keyword'>throws</span> ServletException {
    <span class='keyword'>super</span>.init(aConfig);
    fLogger.config(<span class='literal'>"WelcomeFile Controller - starting."</span>);
    fDestination = aConfig.getInitParameter(<span class='literal'>"Destination"</span>);
    <span class='keyword'>if</span> ( ! Util.textHasContent(fDestination) ) {
      fLogger.severe(<span class='literal'>"Destination URL needed, but not configured in web.xml."</span>);
    }
  }
  
  <span class='keyword'>@Override</span> <span class='keyword'>public</span> <span class='keyword'>void</span> destroy(){
    fLogger.config(<span class='literal'>"WelcomeFile Controller - ending."</span>);
  }
  
  <span class='keyword'>@Override</span> <span class='keyword'>protected</span> <span class='keyword'>void</span> doGet(
    HttpServletRequest aRequest, HttpServletResponse aResponse
  ) <span class='keyword'>throws</span> ServletException, IOException {
    fLogger.fine(<span class='literal'>"Redirecting directory request to new location : "</span> + fDestination);
    aResponse.setContentType(<span class='literal'>"text/html"</span>);
    aResponse.sendRedirect(fDestination);
  }
  
  <span class='comment'>// PRIVATE //
</span>  <span class='keyword'>private</span> String fDestination;
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> Logger fLogger = Util.getLogger(WelcomeFileController.<span class='keyword'>class</span>);
}
 
</PRE>
<br>
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicAction8130-2.html?Id=181'>Forward versus redirect</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='219'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=219 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:26:57 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
